<?php
// +----------------------------------------------------------------------
// | 火凤凰CMS内容管理系统
// +----------------------------------------------------------------------
// | Copyright (c) 2015~2018 http://cxbs.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 赵志广 <amdin@cxbs.net>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\common\controller\Adminbase;
use app\common\model\OrderMake as cxModel;
use app\common\model\Smscode;
use app\common\model\UserAddress;

class OrderMake extends Adminbase {
    //  首页
    public function index(){
        $cxmodel = new cxModel();
        //  定义时间
        $parttime['jinadd'] = strtotime(date("Y-m-d 00:00:00",time()));
        $parttime['jinend'] = time();
        $parttime['zuoadd'] = strtotime(date("Y-m-d 00:00:00",strtotime("-1 day")));
        $parttime['zuoend'] = strtotime(date("Y-m-d 23:59:59",strtotime("-1 day")));
        $parttime['zhouadd'] = strtotime(date("Y-m-d 00:00:00",strtotime("-7 day")));
        //  获取今日预约数量
        $make['yuejinnum'] = $cxmodel->where('addtime','>',$parttime['jinadd'])->where('addtime','<',$parttime['jinend'])->count();
        $make['zuojinnum'] = $cxmodel->where('addtime','>',$parttime['zuoadd'])->where('addtime','<',$parttime['zuoend'])->count();
        //  本周申请取消预约数量
        $make['zhoujinnum'] = $cxmodel->where('status','3')->count();
        //  本周取消预约数量
        $make['zhoucelnum'] = $cxmodel->where('status','4')->where('addtime','>',$parttime['zhouadd'])->where('addtime','<',$parttime['jinend'])->count();
        $make['zhouendnum'] = $cxmodel->where('vipstatus','2')->where('addtime','>',$parttime['zhouadd'])->where('addtime','<',$parttime['jinend'])->count();
        //  获取待分配老师订单
        $make['etcvip'] = $cxmodel->whereOr('vipstatus','not in','1,2')->whereOr('vipstatus','=','null')->count();
        //  获取特约老师订单
        $make['vipnum'] = $cxmodel->where('foid','<>','null')->where('vipstatus','not in','1,2')->count();
        //  获取特约老师订单
        $make['etcnum'] = $cxmodel->where('vipstatus','1')->count();
        //  获取订单
        $makelist = $cxmodel->order('id desc')->paginate('20')->each(function ($item,$key){
            $item->vipname = null;
            if(!empty($item->vipuid)){
                $userdata = model('UserData')->where('uid',$item->vipuid)->find()->toArray();
                $item->vipname = $userdata['uname'];
            }
            $item->arttitle = model('Article')->where('aid',$item->aid)->value('title');
            $item->useraddress = model('UserAddress')->where('id',$item->resid)->find();
            $item->vipmoney = '0';
            $item->paystatus = '1';
            if(!empty($item->foid)){
                $num = model('Order')->where('oid',$item->foid)->count();
                $item->paystatus = '0';
                if($num > '0'){
                    $item->paystatus = model('Order')->where('oid',$item->foid)->value('playstatus');
                }
                $item->vipmoney = $userdata['vipmoney'];
            }
            $item->toidstatus = '1';
            if(!empty($item->toid)){
                $tnum = model('Order')->where('oid',$item->toid)->count();
                $item->toidstatus = '0';
                if($tnum > '0'){
                    $item->toidstatus = model('Order')->where('oid',$item->toid)->value('playstatus');
                }
            }
        });
        $newmakelist = null;
        if(!empty($makelist)){
            foreach ($makelist as $k => $v){
                $v = $v->toArray();
                $newmakelist[$k] = $v;
            }
        }
        $this->assign([
            'make' => $make,
            'makelist' => $newmakelist,
            'status' => '',
        ]);
        return view();
    }
    //  订单列表页
    public function get_list(){
        $cxmodel = new cxModel();
        $getdata = request()->param();
        $getdata = datatrim($getdata);
        if(empty($getdata['status'])){
            $getdata['status'] = 'all';
        }
        if($getdata['status'] == 'all'){
            $makelist = $cxmodel->order('id desc')->paginate(20);
        }elseif($getdata['status'] == 'one'){
            $makelist = $cxmodel->whereOr('status','0')->whereOr('vipstatus','in','0,3')->order('id desc')->paginate(20);
        }else{
            $makelist = $cxmodel->where('status',$getdata['status'])->order('id desc')->paginate(20);
        }
        $page = $makelist->render();
        $newmakelist = null;
        if(!empty($makelist)){
            $usermodel = new \app\common\model\UserData();
            $addresmodel = new UserAddress();
            $artmodel = new \app\common\model\Article();
            $ordermodel = new \app\common\model\Order();
            foreach ($makelist as $k => $v){
                $v = $v->toArray();
                $v['vipname'] = null;
                if(!empty($v['vipuid'])){
                    $userdata = $usermodel->where('uid',$v['vipuid'])->find()->toArray();
                    $v['vipname'] = $userdata['uname'];
                }
                $v['arttitle'] = $artmodel->where('aid',$v['aid'])->value('title');
                $v['useraddress'] = $addresmodel->where('id',$v['resid'])->find();
                $v['vipmoney'] = '0';
                $v['paystatus'] = '1';
                if(!empty($v['foid'])){
                    $num = $ordermodel->where('oid',$v['foid'])->count();
                    $v['paystatus'] = '0';
                    if($num > '0'){
                        $v['paystatus'] = $ordermodel->where('oid',$v['foid'])->value('playstatus');
                    }
                    $v['vipmoney'] = $userdata['vipmoney'];
                }
                $v['toidstatus'] = '1';
                if(!empty($v['toid'])){
                    $tnum = $ordermodel->where('oid',$v['toid'])->count();
                    $v['toidstatus'] = '0';
                    if($tnum > '0'){
                        $v['toidstatus'] = $ordermodel->where('oid',$v['toid'])->value('playstatus');
                    }
                }
                $newmakelist[$k] = $v;
            }
        }
        $this->assign([
            'makelist' => $newmakelist,
            'pages' => $page,
            'status' => $getdata['status'],
        ]);
        return view();
    }
    //  处理课程
    public function edit(){
        $cxmodel = new cxModel();
        $getdata = request()->param();
        $getdata = datatrim($getdata);
        if(empty($getdata['id'])){
            $this->error("非法访问");
        }
        $usermodel = new \app\common\model\UserData();
        $addresmodel = new UserAddress();
        $artmodel = new \app\common\model\Article();
        $ordermodel = new \app\common\model\Order();
        $edit = $cxmodel->where('id',$getdata['id'])->find()->toArray();
        //  标准化预约数据
        $edit['vipname'] = null;
        if(!empty($edit['vipuid'])){
            $userdata = $usermodel->where('uid',$edit['vipuid'])->find()->toArray();
            $edit['vipname'] = $userdata['uname'];
            $edit['vipuser'] = $userdata;
        }
        $edit['arttitle'] = $artmodel->where('aid',$edit['aid'])->value('title');
        $edit['useraddress'] = $addresmodel->where('id',$edit['resid'])->find()->toArray();
        $edit['vipmoney'] = '0';
        $edit['paystatus'] = '1';
        if(!empty($edit['foid'])){
            $num = $ordermodel->where('oid',$edit['foid'])->count();
            $edit['paystatus'] = '0';
            if($num > '0'){
                $edit['paystatus'] = $ordermodel->where('oid',$edit['foid'])->value('playstatus');
            }
            $edit['vipmoney'] = $userdata['vipmoney'];
        }
        $edit['toidstatus'] = '1';
        if(!empty($edit['toid'])){
            $tnum = $ordermodel->where('oid',$edit['toid'])->count();
            $edit['toidstatus'] = $edit['toidmoney'] = '0';
            if($tnum > '0'){
                $toiddata = $ordermodel->where('oid',$edit['toid'])->find()->toArray();
                $edit['toidstatus'] = $toiddata['playstatus'];
                $edit['toidmoney'] = $toiddata['pricemoney'];
            }
        }
        if(request()->isPost()){
            $data = input('post.');
            $data = datatrim($data);
            $sms = false;
            $vipphone = $makephone = null;
            if($data['class'] == '1'){
                $data['status'] = '1';
                $data['vipstatus'] = '0';
                if(!empty($data['sms'])){
                    $sms = true;
                    if(!empty($data['vipuid'])){
                        $smsdata = model('UserData')->where('uid',$data['vipuid'])->find();
                    }else{
                        $smsdata = $edit['vipuser'];
                    }
                    foreach ($data['sms'] as $k => $v){
                        if($k == 'laoshi'){
                            $vipphone = $smsdata['uphone'];
                        }
                        if($k == 'xuanyue'){
                            $makephone = $edit['useraddress']['phone'];
                        }
                    }
                }
            }
            if($data['class'] == '2'){
                $data['vipstatus'] = '0';
            }

            if($cxmodel->allowField(true)->isUpdate(true)->save($data,['id'=>$data['id']])){
                if($sms){
                    $smsmodel = new Smscode();
                    if(!empty($vipphone)){
                        $lstime = date("Y-m-d H:i:s",$edit['maketime']);
                        $laoshisms = array(
                            'phone' => $vipphone,
                            'title' => "{$smsdata['uname']}课程通知",
                            'tpcode' => 'SMS_150577143',
                            'code' => "亲爱的{$smsdata['uname']}，您在{$lstime}有新的课程安排，请及时查看课程详情。",
                            'cont' => array(
                                'username' => $smsdata['uname'],
                                'time' => $lstime,
                            ),
                        );
                        $smsmodel->smsnotice($laoshisms);
                    }
                    if(!empty($makephone)){
                        $xytime = date("Y-m-d H:i:s",$edit['maketime']);
                        $xuanyuansms = array(
                            'phone' => $vipphone,
                            'title' => "{$edit['useraddress']['adduname']}课程通知",
                            'tpcode' => 'SMS_150572215',
                            'code' => "亲爱的{$edit['useraddress']['adduname']}，您预约{$xytime}的课程，已安排{$smsdata['uname']}（手机：{$smsdata['uphone']}）为您服务。",
                            'cont' => array(
                                'username' => $edit['useraddress']['adduname'],
                                'shijian' => $xytime,
                                'laoshi' => $smsdata['uname'],
                                'laoshiphone' => $smsdata['uphone'],
                            ),
                        );
                        $smsmodel->smsnotice($xuanyuansms);
                    }
                }
                $this->success("处理成功！");
            }else{
                $this->error("处理失败！");
            }
        }
//        halt($edit);
        $this->assign([
           'postdb' => $edit,
        ]);
        return view();
    }
}